package py.com.alfa.escriba.gui.rest.services.resttemplate;

import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;

import py.com.alfa.escriba.gui.config.EscribaConfigApplicationContextProvider;
import py.com.alfa.escriba.gui.dto.gestion.InmueblesDTO;
import py.com.alfa.escriba.gui.rest.services.InmueblesService;
import py.com.alfa.escriba.gui.services.Services;
import py.com.sif.rest.client.spring.request.ClientRequestBuilder;
@SuppressWarnings({ "unchecked"})
public class InmueblesServiceClient implements InmueblesService {
	
	private Logger log = LoggerFactory.getLogger(getClass());
	
	public InmueblesServiceClient() {}

	@Override
	public List<InmueblesDTO> buscarInmuebles(InmueblesDTO inmueble)
			throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(inmueble);
		ResponseEntity<InmueblesDTO[]> response = (ResponseEntity<InmueblesDTO[]>) request.exchange(BUS_INMUE, HttpMethod.POST, InmueblesDTO[].class);
		log.info("OK - buscarInmuebles ");
		List<InmueblesDTO> result = request.getArrayAsList(response.getBody());
		return result;
	}

	@Override
	public InmueblesDTO createInmueble(InmueblesDTO inmueble)throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(inmueble);
		ResponseEntity<InmueblesDTO> response = (ResponseEntity<InmueblesDTO>) request.exchange(CREA_INMUE, HttpMethod.POST, InmueblesDTO.class);
		log.info("OK - createInmueble ");
		InmueblesDTO result = response.getBody();
		return result;
	}

	@Override
	public InmueblesDTO saveInmueble(InmueblesDTO inmueble) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", inmueble.getId().toString());
		request.addBody(inmueble);
		ResponseEntity<InmueblesDTO> response = (ResponseEntity<InmueblesDTO>) request.exchange(UPD_INMUE, HttpMethod.PUT, InmueblesDTO.class);
		log.info("OK - saveInmueble ");
		InmueblesDTO result = response.getBody();
		return result;
	}

	@Override
	public void deleteInmueble(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		request.exchange(DEL_INMUE, HttpMethod.DELETE, null);
		log.info("OK - deleteInmueble ");
		
	}

	@Override
	public InmueblesDTO showInmueble(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		ResponseEntity<InmueblesDTO> response = (ResponseEntity<InmueblesDTO>) request.exchange(GET_INMUE, HttpMethod.GET, InmueblesDTO.class);
		log.info("OK - showInmueble ");
		InmueblesDTO result = response.getBody();
		return result;
	}

}
